package com.bskj.kuaifu.service.template.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bskj.common.bean.model.WechatTemplateMsg;
import com.bskj.common.bean.model.WxTemplateParams;
import com.bskj.common.utils.DateTimeUtil;
import com.bskj.common.utils.LogUtils;
import com.bskj.kuaifu.bean.vo.resp.UserParkInfoRespVo;
import com.bskj.kuaifu.bean.vo.resp.UserUnPayDetailRespVo;
import com.bskj.kuaifu.service.IParkInfoService;
import com.bskj.kuaifu.service.template.TemplateMessageAction;

/**
 * Created by mocaijin on 21/12/2017.
 *{{first.DATA}}
 *订单金额：{{keyword1.DATA}}
 *停车地点：{{keyword2.DATA}}
 *车牌号码：{{keyword3.DATA}}
 *停车时间：{{keyword4.DATA}}
 *{{remark.DATA}}
 *
 */
@Service(value = "unPayOrderAction")
public class UnPayOrderAction implements TemplateMessageAction {

    @Autowired
    private IParkInfoService parkInfoService;

    @Override
    public WechatTemplateMsg processMessage(Map<String,Object> paramMap) throws Exception {
    	Integer userId = (Integer)paramMap.get("userId");
        UserUnPayDetailRespVo unpayDetail = parkInfoService.getUnPayDetail(userId);
        LogUtils.debug("unpayDetail:" + JSONObject.toJSONString(unpayDetail));
        //欠费的停车单
        List<UserParkInfoRespVo> arrearsParkInfoList = unpayDetail.getArrearsParkInfoList();
        UserParkInfoRespVo latestDetail = new UserParkInfoRespVo();
        WechatTemplateMsg wechatTemplateMsg = new WechatTemplateMsg();
        WxTemplateParams params = new WxTemplateParams();
        if (arrearsParkInfoList != null && arrearsParkInfoList.size() >= 1) {
            latestDetail = arrearsParkInfoList.get(0);
            arrearsParkInfoList.remove(latestDetail);
        }

        if (latestDetail.getOutTime() == null) {
            latestDetail.setOutTime(new Date());
        }

        params.put("first", "您共有以下订单需要立即支付");
        params.put("keyword1", latestDetail.getOweAmount() / 10 + ".00元");
        params.put("keyword2", latestDetail.getParkLotName());
        params.put("keyword3", latestDetail.getCarNo());
        params.put("keyword4", DateTimeUtil.date2String(latestDetail.getOutTime()) + "");
        String templateMsg = getTemplateMsgFromList(arrearsParkInfoList);
        templateMsg += getTemplateMsgFromList(unpayDetail.getUnPayParkInfoList());
        params.put("remark", templateMsg + "\n支付完成后才可继续享受停车场直通服务及折扣优惠");
        wechatTemplateMsg.setData(params);

        return wechatTemplateMsg;
    }

    private String getTemplateMsgFromList(List<UserParkInfoRespVo> arrearsParkInfoList) throws Exception{
        if (arrearsParkInfoList == null) {
            return "";
        }
        String templateMsg = "";
        for (UserParkInfoRespVo item : arrearsParkInfoList) {
            templateMsg += getTemplateMsg(item.getParkLotName(), DateTimeUtil.date2String(item.getOutTime()) + "", item.getCarNo(), item.getOweAmount() / 10 + "");
        }
        return templateMsg;
    }

    private String getTemplateMsg(String parkingName, String orderTime,String carNo, String orderAmount) {
        String templateMsg = "\n订单金额：<OrderAmount> \n停车地点：<ParkingName> \n车牌号码：<CarNo> \n停车时间：<OrderTime>\n";
        templateMsg = templateMsg.replace("<ParkingName>", parkingName);
        templateMsg = templateMsg.replace("<OrderTime>", orderTime);
        templateMsg = templateMsg.replace("<CarNo>", carNo);
        templateMsg = templateMsg.replace("<OrderAmount>", orderAmount+ ".00元");

        LogUtils.debug("templateMsg:" + templateMsg);
        return templateMsg;

    }
}
